package org.reference.generic.operator;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.reference.generic.base.Parameter;

public class NodeFactory {
	public static Node getRandomNode(int maxDepth, int depth, int arity) {
		if(maxDepth == depth) {
			Parameter[] params = createTerminalParams();
			return new Terminal(params); 
		} else {
			Parameter[] params = createFunctionParams(maxDepth, depth, arity);
			return getFunction(params);
		}
	}

	private static Parameter[] createTerminalParams() {
		Parameter[] params = {
			new Parameter(Parameter.TYPE_TERM_VALUE, Parameter.TYPE_VALUE_CT),
			new Parameter(Parameter.TYPE_TERM_VALUE, Parameter.TYPE_VALUE_PT),
			new Parameter(Parameter.TYPE_TERM_VALUE, Parameter.TYPE_VALUE_RM)
		};
		return params;
	}

	private static Parameter[] createFunctionParams(int maxDepth, int depth, int arity) {
		Parameter[] params = {
				new Parameter(Parameter.TYPE_MAX_DEPTH, String.valueOf(maxDepth)), 
				new Parameter(Parameter.TYPE_DEPTH, String.valueOf(depth)), 
				new Parameter(Parameter.TYPE_ARITY, String.valueOf(arity))
		};
		return params;
	}
	
	private static Node getFunction(Parameter[] params) {
		List<Node> nodes = new ArrayList<Node>();
		nodes.add(new Add(params));
		nodes.add(new Sub(params));
		nodes.add(new Div(params));
		nodes.add(new Mul(params));
		Collections.shuffle(nodes);
		return nodes.get(0);
	}
}
